package com.decent.controller;

import com.decent.entity.Store;
import com.decent.entity.common.Message;
import com.decent.service.OperationLogService;
import com.decent.service.StoreService;
import com.decent.util.MessageEnum;
import com.decent.util.MessageUtilEnum;
import com.decent.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @Author decent
 * @Create 2021/3/31 - 10:48
 * @Description
 */
@RestController
public class StoreController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private OperationLogService operationLogService;

    @PostMapping("getStoreList")
    public Message getStoreList() {
        List<Store> storeList = storeService.getStoreList();
        return MessageUtils.buildMsg(MessageEnum.SELECT_STORE_SUCCESS, storeList);
    }

    @RequestMapping("insertStore")
    public Message insertStore(@Valid Store store) {

        if (store == null) {
            return MessageUtils.buildMsg(MessageEnum.PARAM_ERROR);
        }
//        operationLogService.insert("/insertStore/" + store.toString());
        Integer count = storeService.insertStore(store);

        return MessageUtils.buildMsg(count > 0, MessageUtilEnum.INSERT_ENUM);
    }

    /**
     * 修改门店的功能，修改门店名称的时候同步修改对应的订单信息
     * @param store id,name
     * @return Message
     */
    @RequestMapping("updateStore")
    public Message updateStore(@Valid Store store) {
        if (store.getStoreId() == null || store.getStoreName() == null) {
            return MessageUtils.buildMsg(MessageEnum.PARAM_ERROR);
        }
//        operationLogService.insert("/updateStore/" + store.toString());
        storeService.updateStore(store);
        return MessageUtils.buildMsg(MessageEnum.UPDATE_SUCCESS);
    }

    @RequestMapping("deleteStore")
    public Message deleteStore(Integer storeId) {
        if (storeId <= 0) {
            return MessageUtils.buildMsg(MessageEnum.PARAM_ERROR);
        }
//        operationLogService.insert("/deleteStore/" + storeId);
        Integer count = storeService.deleteStore(storeId);
        return MessageUtils.buildMsg(count > 0, MessageUtilEnum.DELETE_ENUM);
    }

}
